package com.librelato.persistencia;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.librelato.bean.CondicaoPagamentoBean;
import com.librelato.persistencia.config.SQLConfiguracao;
import com.librelato.persistencia.enums.EnumCondicao;
import com.librelato.persistencia.interfaces.ICondicaoPagamentoDao;

public class CondicaoPagamentoDao<P extends CondicaoPagamentoBean> extends
		DaoGenerico<P> implements ICondicaoPagamentoDao<P> {

	@Override
	public String procuraPorParam(String tabela, String campo, String valor,
			EnumCondicao condicao) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public P salvar(P p) throws Exception {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public void excluir(P p) throws Exception {
		// TODO Auto-generated method stub

	}

	@Override
	public void editar(P p) throws Exception {
		// TODO Auto-generated method stub

	}

	@Override
	public void preSalvar(P p) {
		// TODO Auto-generated method stub

	}

	@SuppressWarnings("unchecked")
	public List<P> buscaCondicaoPagamento(Map<String, String> param)
			throws Exception {
		List<CondicaoPagamentoBean> condicoes = new ArrayList<CondicaoPagamentoBean>();
		CondicaoPagamentoBean condicao = null;

		try {

			conn = GerenciadorConexao.getConexao();
			query = SQLConfiguracao.getInstance().getValor("localiza_condicao");
			int psIndex = 1;
			if (param.get("CODIGO") != null) {
				query += " AND E4_CODIGO LIKE ?";
			}
			if (param.get("DESCRICAO") != null) {
				query += " AND E4_DESCRI LIKE ?";
			}
			query += " ORDER BY E4_CODIGO";
			ps = conn.prepareStatement(query);
			if (param.get("CODIGO") != null) {
				ps.setString(psIndex++, param.get("CODIGO").trim()
						.toUpperCase()
						+ "%%");
			}
			if (param.get("DESCRICAO") != null) {
				ps.setString(psIndex++, "%%"
						+ param.get("DESCRICAO").trim().toUpperCase() + "%%");
			}
			rs = ps.executeQuery();
			while (rs.next()) {
				condicao = new CondicaoPagamentoBean();
				condicao.setCodigo(rs.getString("E4_CODIGO"));
				condicao.setCondicao(rs.getString("E4_COND"));
				condicao.setDescricao(rs.getString("E4_DESCRI"));
				condicoes.add(condicao);
			}

		} catch (Exception e) {
			throw e;
		}

		return (List<P>) condicoes;
	}

	public CondicaoPagamentoBean buscaCondicaoPagamento(String codigo)
			throws Exception {
		CondicaoPagamentoBean condicao = null;
		try {
			conn = GerenciadorConexao.getConexao();
			query = SQLConfiguracao.getInstance().getValor("localiza_condicao");
			query += " AND E4_CODIGO = ?";
			query += " ORDER BY E4_CODIGO";
			ps = conn.prepareStatement(query);
			ps.setString(1, codigo);
			rs = ps.executeQuery();
			while (rs.next()) {
				condicao = new CondicaoPagamentoBean();
				condicao.setCodigo(rs.getString("E4_CODIGO"));
				condicao.setCondicao(rs.getString("E4_COND"));
				condicao.setDescricao(rs.getString("E4_DESCRI"));
			}
		} catch (Exception e) {
			throw e;
		}
		return condicao;
	}

}
